<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 集采客户
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class ShopCustomerController extends CommonController
{
    
    public function index(){
        $map = [];
        list($list, $page) = $this->lists('ShopCustomer', $map, 'id desc');
        if($list){
            // 店铺
            if($shop_ids = array_column($list, 'shop_id')){
                $shop_arr = M('shop')->where(['id' => ['in', $shop_ids]])->getField('id,shopname');
            }else{
                $shop_arr = [];
            }
            foreach ($list as $key => &$row) {
                if(isset($shop_arr[$row['shop_id']])){
                    $row['shop_name'] = $shop_arr[$row['shop_id']];
                }else{
                    $row['shop_name'] = '';
                }
            }
        }
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function edit(){
        $id = I('id');
        if (IS_POST) {
            $data = I('post.');
            $model = D('Api/ShopCustomer');

            if($model->create($data)){
                $res = $model->where(['id'=>$id])->save();
                if($res !== false){
                    $this->ajaxReturn(['s'=>true, 'm'=>'修改成功', 'url' => U('index')]);
                }else{
                    $this->ajaxReturn(['s'=>false, 'm'=>'修改失败']);
                }
            }else{
                $this->ajaxReturn(['s'=>false, 'm'=>$model->getError()]);
            }
        } else {
            $info = M('ShopCustomer')->where(['id' => $id])->find();
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del(){
        $id = I('id');
        $res = M('ShopCustomer')->where(['id' => $id])->delete();
        if ($res) {
            $this->ajaxReturn(array('s' => true, 'm' => '删除成功', 'url' => U('index')));
        } else {
            $this->ajaxReturn(array('s' => false, 'm' => '删除失败!'));
        }
    }

    public function goods(){
        $id = I('id');
        $info = M('ShopCustomer')->where(['id' => $id])->find();
        if(empty($info)){
            $this->error('记录不存在');
        }
        $goods = $info['goods'] ? json_decode($info['goods'], true) : [];
        $this->assign('goods', $goods);
        $this->assign('id', $id);
        $this->display();
    }

    function goods_edit(){
        $id = I('id');
        $key = I('key');
        $info = M('ShopCustomer')->where(['id' => $id])->find();
        if(empty($info)){
            $this->error('记录不存在');
        }
        $goods = $info['goods'] ? json_decode($info['goods'], true) : [];
        if(!isset($goods[$key])){
            $this->error('记录不存在');
        }
        
        if (IS_POST) {
            $data = I('post.');
            $goods[$key] = $data['info'];
            $data = [
                'goods' => json_encode($goods)
            ];
            M('ShopCustomer')->where(['id' => $id])->save($data);
            $this->ajaxReturn(array('status' => true, 'm' => '修改成功', 'url' => U('goods', ['id'=>$id, 'key'=>$key])));
        }
        $this->assign('id', $id);
        $this->assign('key', $key);
        $this->assign('info', $goods[$key]);
        $this->display();
    }

    function goods_del(){
        $id = I('id');
        $key = I('key');
        $info = M('ShopCustomer')->where(['id' => $id])->find();
        if(empty($info)){
            $this->ajaxReturn(array('s' => false, 'm' => '记录不存在'));
        }
        $goods = $info['goods'] ? json_decode($info['goods'], true) : [];
        if(!isset($goods[$key])){
            $this->ajaxReturn(array('s' => false, 'm' => '记录不存在'));
        }
        unset($goods[$key]);
        $data = [
            'goods' => json_encode($goods)
        ];
        M('ShopCustomer')->where(['id' => $id])->save($data);
        $this->ajaxReturn(array('s' => true, 'm' => '删除成功', 'url' => U('index')));
    }
}